package net.hnedu.search.resource;

import java.util.TimerTask;

import net.hnedu.map.ResResource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class AddUpdateResourceIndex extends TimerTask {
  private static Log log = LogFactory.getLog(AddUpdateResourceIndex.class);

  //Constants used for operations
  public static final int OPERATION_ADD = 0;
  public static final int OPERATION_UPDATE = 1;

  private ResResource res;
  private int operation;

  AddUpdateResourceIndex(ResResource res, int operation) {
    this.res = res;
    this.operation = operation;
  }

  public void run() {
    log.debug("AddUpdateResourceIndex.run : op = " + operation +
              " for resId = " + res.getResId());
    try {
      switch (operation) {
        case OPERATION_UPDATE:
          ResourceIndex.deleteResFromIndex(res.getResId());
          // fall through
        case OPERATION_ADD:
          ResourceIndex.addResToIndex(res);
          break;
        default:
          log.warn(
              "Cannot process the AddUpdatePostIndexTask with operation = " +
              operation);
      }
    }
    catch (Exception ex) {
      log.error("Error while performing index operation", ex);
    }
  }


}
